<?php

namespace Database\Factories;

use App\Models\Series;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Series>
 */
class SeriesFactory extends Factory
{
    protected $model = Series::class;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $name = $this->faker->randomElement([
            'Vue.js 从入门到精通',
            'Laravel 实战教程',
            '前端开发最佳实践',
            '全栈开发指南',
            '设计模式详解',
            '算法与数据结构',
            '微服务架构实践',
            'DevOps 实战',
        ]);

        $slug = Str::slug($name) . '-' . Str::random(6);

        // 使用中文 Faker 生成描述
        $faker = \Faker\Factory::create('zh_CN');
        
        return [
            'name' => $name,
            'slug' => $slug,
            'description' => $faker->paragraph(3),
            'featured_image' => 'https://dummyimage.com/800x400/6366f1/ffffff&text=' . urlencode($name),
            'author_id' => User::factory(),
            'post_count' => 0,
            'is_visible' => true,
            'meta_title' => $name,
            'meta_description' => $faker->sentence(),
        ];
    }

    /**
     * 设置为不可见
     */
    public function hidden(): static
    {
        return $this->state(function (array $attributes) {
            return [
                'is_visible' => false,
            ];
        });
    }

    /**
     * 设置作者
     */
    public function authoredBy(User $user): static
    {
        return $this->state(function (array $attributes) use ($user) {
            return [
                'author_id' => $user->id,
            ];
        });
    }
}
